package com.qinghe.plant.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qinghe.plant.dao.Task;
import com.qinghe.plant.model.TaskResponsitory;
import com.qinghe.plant.util.CodeType;
import com.qinghe.plant.util.Response;

@RestController
public class ScadaController{
	
	@Autowired
	private TaskResponsitory taskResponsitory;
	
	@RequestMapping(value = "doReadTasks", method = {RequestMethod.GET})
    public Response ReadTasks(HttpServletRequest request) 
	{
		List<Task> tasks=taskResponsitory.findAll();		
		String content=JSONArray.toJSONString(tasks).toString();
		return new Response(CodeType.Success,content,"操作成功");
	}
	
	@RequestMapping(value = "doReadTask", method = {RequestMethod.POST})
    public Response ReadTask(HttpServletRequest request) 
	{
		String id=request.getParameter("id");
		Task task=FindTaskById(Integer.parseInt(id));
		return new Response(CodeType.Success,JSONObject.toJSONString(task),"操作成功");
	}
	
	@RequestMapping(value = "doAlterTask", method = {RequestMethod.POST})
	public Response AlterTask(HttpServletRequest request){		
		Task task=JSONObject.parseObject(request.getParameter("task"),Task.class);
		task= taskResponsitory.save(task);
		return new Response(CodeType.Success,JSONObject.toJSONString(task),"操作成功");
	}

	@RequestMapping(value = "doInsertTask", method = {RequestMethod.POST})
	public Response InsertTask(HttpServletRequest request)
	{		
		Task task=new Task();
		task.setOid(request.getParameter("WorkOrderId"));
		task.setPid(request.getParameter("WorkRouteId"));
		task.setEid(request.getParameter("WorkCenterNme"));
		task.setName(request.getParameter("WorkerName"));
		task.setCount(Double.parseDouble(request.getParameter("Qty")));
		task.setState(request.getParameter("Status"));
		task.setTimer(new Date());
		task= taskResponsitory.save(task);
		return new Response(CodeType.Success,JSONObject.toJSONString(task),"操作成功");	
	}
	
	@RequestMapping(value = "doDeleteTask", method = {RequestMethod.POST})
	public Response DeleteTask(HttpServletRequest request)
	{
		Task task=JSONObject.parseObject(request.getParameter("task"),Task.class);
		taskResponsitory.deleteById(task.getId());	
		return new Response(CodeType.Success,null,"操作成功");
	}	
	
	public void InsetData()
	{
		System.out.println("插入一次Scada数据");
	}
	
	public void DeleteData(Date date)
	{
		System.out.println("删除一次Scada数据");
	}
	
	private Task FindTaskById(Integer id) {
		@SuppressWarnings("serial")
		List<Task> result = taskResponsitory.findAll(new Specification<Task>() {
			@Override
			 public Predicate toPredicate(Root<Task> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();

				list.add(cb.equal(root.get("id").as(Integer.class), id));
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
				}
		});
		return (result.size()!=0)?result.get(0):null;
	}		
}
